package com.jsnu.xiaochi.controller;

import com.jsnu.xiaochi.pojo.User;
import com.jsnu.xiaochi.service.RegisterService;
import com.jsnu.xiaochi.util.GetUUID;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

@Controller
public class RegisterController {
    @Resource
    RegisterService registerService;
    @Value("${NGINX_HTTP}")
    private String NGINX_HTTP;

    @RequestMapping("register")
    public String showRegister(){
        return "register";
    }

    @RequestMapping("userRegister")
    @ResponseBody
    public String userRegister(String username, String phone, String password){
        String salt = GetUUID.getUUID().substring(0,20);
        System.out.println(salt);
        User user=new User();
        user.setUsername(username);
        user.setSalt(salt);
        user.setTelphone(phone);
        user.setImg(NGINX_HTTP+"/xiaochi/avatar.jpg");
        user.setSex("男");
        user.setPassword(new SimpleHash("md5",password, salt,3).toString());
        System.out.println(user);
        try {
            registerService.userRegister(user);
            return "success";//返回1为成功
        }catch (Exception e){
            return "fail";
        }
    }
}